﻿namespace Pp.UI.Controllers
{
    using System;
    using System.Drawing;
    using System.Web.Mvc;
    using Dto;
    using Globalization;
    using Models;
    using Repository;

    [Authorize]
    public class AngelController : BaseController
    {
        public ActionResult Index(int? page, Guid? id)
        {
            if (!id.HasValue) return RedirectToAction("Index", "Home");
            var angels = AngelRepository.GetAngelListById(page, id.Value);
            if (!angels.Success) return View("Error", angels.Error);
            ViewData["Title"] = Res.Title_Angel_List;
            return View(angels.Data);
        }

        public ActionResult Create(Guid? id)
        {
            ViewData["Title"] = Res.Title_New_Angel;
            return View(GetCreateViewModel(new AngelCreate(), id));
        }

        public AngelCreateViewModel GetCreateViewModel(AngelCreate angelCreate, Guid? id)
        {
            var projects = ProjectRepository.GetAllById(id);
            var viewModel = new AngelCreateViewModel
                                {
                                    AngelCreate = angelCreate,
                                    Projects = new SelectList(projects.Data, "Id", "Name", id)
                                };
            return viewModel;
        }
        [HttpPost, ValidateAntiForgeryToken,ValidateInput(false)]
        public ActionResult Create(AngelCreate angelCreate)
        {
            if (ModelState.IsValid)
            {
                var result = AngelRepository.Create(angelCreate);
                if (result.Success)
                {

                    Flash(Res.Message_Angel_Created_Successful, MessageType.Success);
                    return RedirectToAction("Index", "Angel", new { Id = angelCreate.ProjectId });
                }
                Flash(result.Error, MessageType.Failed);
            }
            return View(GetCreateViewModel(angelCreate, angelCreate.ProjectId));
        }


        public ActionResult Edit(Guid? id)
        {
            ViewData["Title"] = Res.Label_Edit_Angel ;
            var edit = AngelRepository.GetEdit(id);
            return View(edit);
        }

        [HttpPost, ValidateAntiForgeryToken, ValidateInput(false)]
        public ActionResult Edit(AngelEdit angelEdit)
        {
            if (ModelState.IsValid)
            {
                var result = AngelRepository.Edit(angelEdit);
                if (result.Success)
                {
                    Flash(Res.Message_Angel_Edited_Successfull, MessageType.Success);
                    return RedirectToAction("Index", "Angel", new { Id = angelEdit.ProjectId });
                }
                Flash(result.Error, MessageType.Failed);
            }
            return View(angelEdit);
        }
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult NewRequest(Guid? projectId)
        {
            return Json(AngelRequestRepository.Create(ProjectRepository.Get(projectId), UserRepository.GetByUsername(User.Identity.Name)));
        }
        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult CancelRequest(Guid? angelRequest, string returnUrl)
        {
            var result = AngelRequestRepository.Cancel(angelRequest);
            if (Request.IsAjaxRequest())
                return Json(result);
            return Redirect(returnUrl);
        }
        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult ApproRequest(Guid? angelRequest, string returnUrl)
        {
            var result = AngelRequestRepository.Appro(angelRequest);
            if (Request.IsAjaxRequest())
                return Json(result);
            return Redirect(returnUrl);
        }


        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Delete(Guid? id, string returnUrl)
        {
            if (string.IsNullOrEmpty(returnUrl)) return RedirectToAction("Index", "Home");
            var result = AngelRepository.DeleteById(id);
            if (result.Success)
            {
                Flash(Res.Message_Drop_Gallery_Successfull, MessageType.Success);
            }
            else { Flash(result.Error, MessageType.Failed); }
            return Redirect(returnUrl);
        }


    }
}
